﻿///////////////////////////////////////////////////////////////////////////////
//
//  This file is part of MathLib.NET.
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//  
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//  
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library;  If not, see 
//  <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////

using MathLib;
using ILNumerics;
using NUnit.Framework;

namespace MathLibTest
{
    [TestFixture]
    public class ind2subTest
    {
        [Test]
        public void Example1()
        {
            ILArray<int> IND = new ILArray<int>(new int[] { 2, 3, 4, 5 }, new int[] { 4 });
            int[] siz = new int[] { 3, 3 };
            ILArray<int>[] answer = new ILArray<int>[] {
                new ILArray<int>(new int[] { 2, 0, 1, 2 }, new int[] { 4 }),
                new ILArray<int>(new int[] { 0, 1, 1, 1 }, new int[] { 4 })
            };

            ILArray<int>[] test_res = MLMath.ind2sub(siz, IND);

            Assert.IsTrue(TestUtils.ArrayEqual(answer, test_res));
        }

        [Test]
        public void Example2()
        {
            ILArray<int> IND = new ILArray<int>(new int[] { 2, 3, 4, 5 }, new int[] { 2, 2 });
            int[] siz = new int[] { 2, 2, 2 };
            ILArray<int>[] answer = new ILArray<int>[] {
                new ILArray<int>(new int[] { 0, 1, 0, 1 }, new int[] { 2, 2 }),
                new ILArray<int>(new int[] { 1, 1, 0, 0 }, new int[] { 2, 2 }),
                new ILArray<int>(new int[] { 0, 0, 1, 1 }, new int[] { 2, 2 })
            };

            ILArray<int>[] test_res = MLMath.ind2sub(siz, IND);

            Assert.IsTrue(TestUtils.ArrayEqual(answer, test_res));
        }

        [Test]
        public void Example3()
        {
            ILArray<int> IND = new ILArray<int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }, new int[] { 8 });
            int[] siz = new int[] { 2, 2, 2 };
            ILArray<int>[] answer = new ILArray<int>[] {
                new ILArray<int>(new int[] { 0, 1, 0, 1, 0, 1, 0, 1 }, new int[] { 8 }),
                new ILArray<int>(new int[] { 0, 0, 1, 1, 0, 0, 1, 1 }, new int[] { 8 }),
                new ILArray<int>(new int[] { 0, 0, 0, 0, 1, 1, 1, 1 }, new int[] { 8 })
            };

            ILArray<int>[] test_res = MLMath.ind2sub(siz, IND);

            Assert.IsTrue(TestUtils.ArrayEqual(answer, test_res));
        }
    }

    public class sub2indTest
    {
        [Test]
        public void reverseExample1()
        {
            ILArray<int> IND = new ILArray<int>(new int[] { 2, 3, 4, 5 }, new int[] { 4 });
            int[] siz = new int[] { 3, 3 };
            ILArray<int>[] answer = new ILArray<int>[] {
                new ILArray<int>(new int[] { 2, 0, 1, 2 }, new int[] { 4 }),
                new ILArray<int>(new int[] { 0, 1, 1, 1 }, new int[] { 4 })
            };

            ILArray<int>[] Sub = MLMath.ind2sub(siz, IND);

            ILArray<int> test_res = MLMath.sub2ind(siz, Sub);
            
            Assert.IsTrue(IND.Equals(test_res));
        }

        [Test]
        public void reverseExample2()
        {
            ILArray<int> IND = new ILArray<int>(new int[] { 2, 3, 4, 5 }, new int[] { 2, 2 });
            int[] siz = new int[] { 2, 2, 2 };
            ILArray<int>[] answer = new ILArray<int>[] {
                new ILArray<int>(new int[] { 0, 1, 0, 1 }, new int[] { 2, 2 }),
                new ILArray<int>(new int[] { 1, 1, 0, 0 }, new int[] { 2, 2 }),
                new ILArray<int>(new int[] { 0, 0, 1, 1 }, new int[] { 2, 2 })
            };

            ILArray<int>[] Sub = MLMath.ind2sub(siz, IND);

            ILArray<int> test_res = MLMath.sub2ind(siz, Sub);

            Assert.IsTrue(IND.Equals(test_res));
        }

        [Test]
        public void reverseExample3()
        {
            ILArray<int> IND = new ILArray<int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }, new int[] { 8 });
            int[] siz = new int[] { 2, 2, 2 };
            ILArray<int>[] answer = new ILArray<int>[] {
                new ILArray<int>(new int[] { 0, 1, 0, 1, 0, 1, 0, 1 }, new int[] { 8 }),
                new ILArray<int>(new int[] { 0, 0, 1, 1, 0, 0, 1, 1 }, new int[] { 8 }),
                new ILArray<int>(new int[] { 0, 0, 0, 0, 1, 1, 1, 1 }, new int[] { 8 })
            };

            ILArray<int>[] Sub = MLMath.ind2sub(siz, IND);

            ILArray<int> test_res = MLMath.sub2ind(siz, Sub);

            Assert.IsTrue(IND.Equals(test_res));
        }
    }
}
